
// project      : soapi-notify 
// purpose      : console and growl notifications of new Stack Exchange questions
// source       : http://soapi.info/tools/notify.aspx
// announcement : http://stackapps.com/questions/820
// website      : http://soapi.info/tools/notify.aspx
// license      : MIT/X11 - Copyright (c) 2010 Sky Sanders - See License.txt
// inspiration  : http://stackapps.com/questions/817/senotify-se-new-question-notification
// 
//  

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Growl.DisplayStyle;
using Soapi.Notify.Displays.Default.Properties;

namespace Soapi.Notify.Displays.Default
{
    public partial class SoapiSettingsPanel : SettingsPanelBase
    {
        private static readonly Point TopLeft = new Point(19, 14);
        private static readonly Point TopRight = new Point(81, 14);
        private static readonly Point BottomLeft = new Point(17, 75);
        private static readonly Point BottomRight = new Point(82, 75);

        private Point _overlayPosition = TopRight;

        public SoapiSettingsPanel()
        {
            InitializeComponent();
        }

        private void SmokestackSettingsPanel_Load(object sender, EventArgs e)
        {
            computerScreenPictureBox.Image = Resources.My_Computer;
            _overlayPosition = GetLocation();

            computerScreenPictureBox.Paint += ComputerScreenPictureBoxPaint;
        }

        private void ComputerScreenPictureBoxPaint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(Resources.overlay, _overlayPosition);
        }

        private Point GetLocation()
        {
            Point p = TopRight;
            Dictionary<string, object> settings = GetSettings();
            if (settings != null && settings.ContainsKey(SoapiDisplay.SettingDisplaylocation))
            {
                try
                {
                    object val = settings[SoapiDisplay.SettingDisplaylocation];
                    int i = Convert.ToInt32(val);
                    switch (i)
                    {
                        case 2:
                            p = TopRight;
                            break;
                        case 3:
                            p = BottomLeft;
                            break;
                        case 4:
                            p = BottomRight;
                            break;
                        default:
                            p = TopLeft;
                            break;
                    }
                }
                catch
                {
                }
            }
            return p;
        }

        private void SaveLocation()
        {
            int i;

            if (_overlayPosition == TopRight) i = 2;
            else if (_overlayPosition == BottomLeft) i = 3;
            else if (_overlayPosition == BottomRight) i = 4;
            else i = 1;

            SaveSetting(SoapiDisplay.SettingDisplaylocation, i);
        }

        private void ComputerScreenPictureBoxMouseClick(object sender, MouseEventArgs e)
        {
            int leftRightLine = computerScreenPictureBox.Width/2;
            int topBottomLine = (computerScreenPictureBox.Height - 40)/2;

            _overlayPosition = TopRight;

            if (e.X > leftRightLine)
            {
                _overlayPosition = (e.Y > topBottomLine ? BottomRight : TopRight);
            }
            else
            {
                _overlayPosition = (e.Y > topBottomLine ? BottomLeft : TopLeft);
            }

            SaveLocation();

            computerScreenPictureBox.Invalidate();
        }
    }
}